<?php

namespace app\user\controller;

use app\common\controller\GlobalVariable;
use app\user\model\User;
//use think\captcha\Captcha;
use think\Controller;
use think\Db;
use think\Request;

/**
 * Class Index 该类下都可以在用户未登录状态下操作
 * @package app\user\controller
 */
class Index extends Controller
{
    /**
     * @return mixed
     * 验证登录
     */
    public function index()
    {
        $token = Request::instance()->header('token');
        $data = Db::name('UserLoginRecord')->where('token', $token)->find();
        if (isset($data['expire_time']) && $data['expire_time'] > time()) {
            return GlobalVariable::promptOkStatus();
        }
        return GlobalVariable::promptErrorByJSON('错误', 401, "未登录!");
    }

    /**
     * 用户登录 login
     */
    public function login()
    {
        $user = new User();
        $nid = input('post.account');
        $pid = input('post.password');
        $isRegister = $user->isRegister($nid);
        if (!$isRegister) {
            return GlobalVariable::promptErrorByJSON("错误", 401, "用户不存在！");
        }
        settype($pid, 'string');
        return $user->doLogin($nid, $pid);
    }


    public function getCaptcha()
    {
        $config = [
            'length' => 3,
            'useNoise' => false,
            'useCurve' => false,
            'useZh' => false
        ];
        $code = new Captcha($config);
        GlobalVariable::switchHTTPStatusCode(200);
        return $code->entry();
    }

    public function caverify()
    {
        $code = input('code');
        if (empty($code)) {
            return GlobalVariable::promptErrorByJSON("错误", 401, "验证码不能为空！");
        }
        // 处理验证码
        if (!captcha_check($code)) {
            return GlobalVariable::promptErrorByJSON("错误", 401, "验证码错误！");
        } else {
            return GlobalVariable::promptOkStatus();
        }
    }

    /**
     * 用户注册
     * @return mixed
     */
    public function register()
    {
        $nid = input('post.account');
        $pid = input('post.password');
        $nickname = input('post.nickname');
        $is_author = input('post.is_author');
        $researcher_id = input('post.researcher_id');
        settype($pid, "string");
        if ($researcher_id == null) {
            $researcher_id = "";
        }
        $orcid = "";
        $identity = "";
        $institution = "";
        $department = "";
        $author_id = "";
        $email = "";
        $phoneNum = "";
        $headImg = "";
        if ($nickname == null) {
            return GlobalVariable::promptErrorByJSON("错误", 406, "请输入昵称!");
        }
        if ($is_author == 1) {
            if ($researcher_id !== "") {
                $ifid = Db::table('xwd_user_info')->where('researcher_id', $researcher_id)->find();
                if ($ifid) {
                    return GlobalVariable::promptErrorByJSON("错误", 406, "ResearcherID已被注册!");
                }
            }
        }
        if (strlen($nid) < 4) {
            return GlobalVariable::promptErrorByJSON("错误", 406, "用户名应大于四位!");
        } elseif (strlen($nid) > 20) {
            return GlobalVariable::promptErrorByJSON("错误", 406, "用户名应小于二十位！");
        }
        if (strlen($pid) < 6) {
            return GlobalVariable::promptErrorByJSON("错误", 406, "密码应大于六位！");
        }
        $user = new User();
        $flag = $user->doRegister($nid, $pid, $nickname, $researcher_id, $is_author, $orcid, $identity, $institution, $department, $author_id, $email, $phoneNum, $headImg);
        if (!$flag) {
            return GlobalVariable::promptErrorByJSON("错误", 403, "用户名已存在");
        }
        return GlobalVariable::promptOkStatus();
    }


}